package com.webchat.ugc.messaegqueue.service;


import com.google.common.collect.Lists;
import com.webchat.domain.dto.queue.MomentPublishMessageDTO;
import com.webchat.domain.vo.response.moment.MomentVO;
import com.webchat.ugc.service.AccountService;
import com.webchat.ugc.service.chain.MomentImageHandler;
import com.webchat.ugc.service.chain.MomentIpAddressHandler;
import com.webchat.ugc.service.chain.MomentLinkHandler;
import com.webchat.ugc.service.chain.MomentPublishHandlerChain;
import com.webchat.ugc.service.chain.MomentRefreshHandler;
import com.webchat.ugc.service.chain.MomentReviewHandler;
import com.webchat.ugc.service.chain.MomentVideoHandler;
import com.webchat.ugc.service.moment.MomentTimeLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MomentPublishConsumeService {

    @Autowired
    private MomentImageHandler momentImageHandler;
    @Autowired
    private MomentVideoHandler momentVideoHandler;
    @Autowired
    private MomentLinkHandler momentLinkHandler;
    @Autowired
    private MomentReviewHandler momentReviewHandler;
    @Autowired
    private MomentRefreshHandler momentRefreshHandler;
    @Autowired
    private MomentIpAddressHandler momentIpAddressHandler;
    @Autowired
    private AccountService accountService;
    @Autowired
    private MomentTimeLineService momentTimeLineService;


    /**
     * 写扩散策略，内容审核质量分超过5，朋友可见，反之仅发帖子人自己可见（降级策略）
     * ps: 基于score可以实现分段策略，比如小红数作品评分在：
     * 10 ~ 40 丢初级流量池
     * 41 ~ 80 丢千人流量池
     * 81 ~ 100 丢万人流量池
     */
    private static final int WRITE_TIME_LINE_SCORE = 5;

    /**
     * 动态发布后置处理
     * @param dto
     */
    public void consume(MomentVO dto) {

        /**
         * 后置动态数据处理链（应用责任链设计模式）：
         *
         * 1. 图片媒体资源信息提取
         * 2. 视频媒体资源信息提取
         * 3. 连接解析
         * 4. ip归属地解析
         * 5. 大模型内容审核
         * 6. 刷新/修改动态状态
         * ……
         */
        /**
         * 动态发布后置处理任务链
         */
        MomentPublishHandlerChain chain = new MomentPublishHandlerChain();
        chain.addHandler(Lists.newArrayList(momentImageHandler,
                                            momentVideoHandler,
                                            momentLinkHandler,
                                            momentIpAddressHandler,
                                            momentReviewHandler,
                                            momentRefreshHandler)).handle(dto, chain);

        /**
         * 写扩散（把当前动态写入到所有粉丝时间线DB、Redis）
         */
        Integer reviewScore = dto.getReviewScore();
        // 大模型内容审核结果质量分
        boolean writeFriends = reviewScore != null && reviewScore > WRITE_TIME_LINE_SCORE;
        momentTimeLineService.addTimeLine(dto.getAuthor(), dto.getId(), dto.getPublishTime(), writeFriends);
    }

}
